////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.util.Linkify;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.IH_UserClient_HttpRequest_Report;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;

/**
 * @author haibo for list item named "Instant Market"
 */
public class CommonContentActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String			TAG	= CommonContentActivity.class.getSimpleName();
	public static String					_title;
	public static Class<? extends Activity>	_cls;

	private TextView						_txt;
	private TextView						_titleTextView;
	private Button							_toolBtnReturn;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_common_content);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");
				((ITDLApplication) getApplicationContext()).putString(CommonDefn.ACTIVITY_CAME_FROM_KEY, TAG);

				Intent intent = new Intent(getApplicationContext(), _cls);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				SwitchForwardActivity(_cls, intent);
			}
		});

		_txt = (TextView) findViewById(R.id.list_item_content);
		_txt.setAutoLinkMask(Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES);
		_titleTextView = (TextView) findViewById(R.id.more_menu_item_title);
	}

	@Override
	protected void onResume()
	{
		SetExectuHttpRequestOnResume(false);

		super.onResume();

		resetTitle();
		
		resetContent();
		
		if(FinanceListActivity.class.getSimpleName().equals(_cls.getSimpleName()))
		{
			_toolBtnReturn.setText(R.string.more_finance_info);
			SentHttpRequestByFinance();
		}
		else if(MoreActivity.class.getSimpleName().equals(_cls.getSimpleName()))
		{
			_toolBtnReturn.setText(R.string.more);
			SendHttpRequest();
		}
	}

	private void resetTitle()
	{
		_titleTextView.setText(_title);
	}
	
	private void resetContent()
	{
		_txt.setTextColor(Color.BLACK);
		_txt.setText("");
	}
	
	private void updateData(List<String[]> result)
	{
		String[] arr = result.get(0);
		
		StringBuffer _newsContent = new StringBuffer();
		try
		{
//			for (String[] array : result)
			for(int i = 0; i < arr.length; i++)
			{
//				String str = array[0];
				String str = arr[i];
				if (str != null && str.trim().length() > 10)
				{
					_newsContent.append(str);
					_newsContent.append("\r\n");
				}
			}
			if (_newsContent.length() < 1)
			{
				_newsContent.append(getResources().getString(R.string.provide_no_information));
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		_txt.setText(_newsContent.toString());
		if (_newsContent.toString().equals(getResources().getString(R.string.provide_no_information))
				|| _newsContent.toString().contains(getResources().getString(R.string.http_error)))
		{
			_txt.setTextColor(Color.RED);
		}
		else
		{
			_txt.setTextColor(Color.BLACK);
		}
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	static public int	iFinanceTypeId	= 0;

	private void SentHttpRequestByFinance()
	{
		ShowHideDialog(GetFinanceDataFromHttpServer());
	}

	private H_CODE GetFinanceDataFromHttpServer()
	{
		return HttpAppInfo.SendFinanceRequest(actContext, iFinanceTypeId, new IH_UserClient_HttpRequest_Report()
		{
			@Override
			public void H_UserClient_HttpRequest_Response(H_UserClient_HttpRequest_Result iResult)
			{
				progressView.DismissProgress();

				if (!iResult.MStatus.equals("0"))
				{
					ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
					return;
				}
				
				updateFinanceData(iResult.contentStores);
			}
		});
	}

	private void updateFinanceData(List<String[]> _result)
	{
		int _selectedItemIndex = iFinanceTypeId;

		StringBuffer _newsContent = new StringBuffer();
		int index = 0;
		for (String[] array : _result)
		{
			index++;
			if (_selectedItemIndex == 1 && array[0] != null)
			{
				_newsContent.append(array[0]);
				_newsContent.append("<br>");
				_newsContent.append("<br>");
			}
			else if (_selectedItemIndex == 2)
			{
				_newsContent.append("<b>" + array[0] + "</b>");
				_newsContent.append("<br>");
				_newsContent.append(array[1]);
				_newsContent.append("<br>");
				_newsContent.append("<br>");
			}
			else if (_selectedItemIndex == 5 || _selectedItemIndex == 6)
			{
				_newsContent.append("<h2>");
				_newsContent.append("〖");
				_newsContent.append(String.format("%02d", index));
				_newsContent.append("〗");
				_newsContent.append("</h2>");
				_newsContent.append(array[0]);
				_newsContent.append("<br>");
				_newsContent.append("<br>");
			}
		}
		
		if (_newsContent.length() < 1)
		{
			_newsContent.append(getResources().getString(R.string.provide_no_information));
		}
		_txt.setText(Html.fromHtml(_newsContent.toString()));
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		return HttpAppInfo.SendIPORequest(actContext, this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(final H_UserClient_HttpRequest_Result iResult)
	{
		if (!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}

		Runnable runnable = new Runnable()
		{
			@Override
			public void run()
			{
				updateData(iResult.contentStores);
			}
		};
		_messageHandler.post(runnable);
	}
}
